/**
 * @file XFooterView.java
 * @create Mar 31, 2012 9:33:43 PM
 * @author Maxwin
 * @description XListView's footer
 */
package com.dreamstations.changshads.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dreamstations.changshads.R;


/**
 * 适用于本recycler的头部下拉刷新view
 */
public class ListViewFooter extends LinearLayout {
    public final static int STATE_NORMAL = 0;
    public final static int STATE_READY = 1;
    public final static int STATE_LOADING = 2;
    public final static int STATE_ERROR = 3;

    private Context mContext;

    private View mContentView;
    private View mProgressBar;
    private TextView mHintView;

    public ListViewFooter(Context context) {
        super(context);
        initView(context);
    }

    public ListViewFooter(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }


    public void setState(int state) {
        mProgressBar.setVisibility(View.INVISIBLE);
//            mHintView.setVisibility(View.INVISIBLE);
        if (state == STATE_READY) {
            mHintView.setVisibility(View.VISIBLE);
            mHintView.setText("松手加载更多");
        } else if (state == STATE_LOADING) {
            mProgressBar.setVisibility(View.VISIBLE);
            mHintView.setVisibility(INVISIBLE);
        } else if(state == STATE_ERROR){
            mProgressBar.setVisibility(GONE);
            mHintView.setVisibility(VISIBLE);
            mHintView.setText("加载失败");
        } else if(state==STATE_NORMAL){
            mProgressBar.setVisibility(View.GONE);
            mHintView.setVisibility(View.GONE);
        }
    }

    public void setBottomMargin(int height) {
        if (height < 0) return ;
        LayoutParams lp = (LayoutParams)mContentView.getLayoutParams();
        lp.bottomMargin = height;
        mContentView.setLayoutParams(lp);
    }

    public int getBottomMargin() {
        LayoutParams lp = (LayoutParams)mContentView.getLayoutParams();
        return lp.bottomMargin;
    }


    /**
     * normal status
     */
    public void normal() {
        mHintView.setVisibility(View.VISIBLE);
        mProgressBar.setVisibility(View.GONE);
    }


    /**
     * loading status
     */
    public void loading() {
        mHintView.setVisibility(View.GONE);
        mProgressBar.setVisibility(View.VISIBLE);
    }

    /**
     * hide footer when disable pull load more
     */
    public void hide() {
        LayoutParams lp = (LayoutParams)mContentView.getLayoutParams();
        lp.height = 1;//这里如果设为0那么layoutManger就会抓不到
        mContentView.setLayoutParams(lp);
        mContentView.setBackgroundColor(Color.BLACK);//这里的颜色要和自己的背景色一致
    }

    /**
     * show footer
     */
    public void show() {
        LayoutParams lp = (LayoutParams)mContentView.getLayoutParams();
        lp.height = LayoutParams.WRAP_CONTENT;
        lp.width =  LayoutParams.MATCH_PARENT;
        mContentView.setLayoutParams(lp);
        mContentView.setBackgroundColor(Color.WHITE);//这里的颜色要和自己的背景色一致
    }

    private void initView(Context context) {
        mContext = context;
        this.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        LinearLayout moreView = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.xlistview_footer, null);
        addView(moreView);
        moreView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        mContentView = moreView.findViewById(R.id.xlistview_footer_content);
        mProgressBar = moreView.findViewById(R.id.xlistview_footer_progressbar);
        mHintView = (TextView)moreView.findViewById(R.id.xlistview_footer_hint_textview);
        mHintView.setText("加载更多。。。");
//            mProgressBar.setVisibility(VISIBLE);//一直会显示转圈，自动加载更多时使用
    }
}

